home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / mui / bcc_src.lha / Parser / ParseBC.cpp.bak < prev    next >
Text File  |  1997-06-22  |  11KB  |  562 lines

  1. #include "ParseBC.h"
  2. #include "ClassDef.h"
  3. #include "MethodDef.h"
  4. #include "VarDef.h"
  5.  
  6. #include "Global.h"
  7.  
  8. #include <string.h>
  9.  
  10. short ParseBC::Start( void )
  11. {
  12.  
  13.     ins_every.Insert( ofh );
  14.     ins_code.Insert( ofh );
  15.     
  16.     switches = 0;
  17.     
  18.     while( 1 ) {
  19.     
  20.         GetToken();
  21.         if( !TokLen ) break;
  22.         
  23.         if( TokType == ALN ) {
  24.             if( TokLen == 6 && !strncmp( "Method", Tok, 6 ) ) {
  25.                 if( !DoMA() ) {
  26.                     if( !ErrorBuf ) Error( 12 );
  27.                     return 0;
  28.                 }
  29.                 switches = 0;
  30.             } else {
  31.             if( TokLen == 7 && !strncmp( "include", Tok, 7 ) ) {
  32.                 if( !DoHeader() ) {
  33.                     if( !ErrorBuf ) Error( 12 );
  34.                     return 0;
  35.                 }
  36.             } else {
  37.             if( TokLen == 9 && !strncmp( "Attribute", Tok, 9 ) ) {
  38.                 if( !DoMA( 1 ) ) {
  39.                     if( !ErrorBuf ) Error( 12 );
  40.                     return 0;
  41.                 }
  42.                 switches = 0;
  43.             } else {
  44.             if( TokLen == 6 && !strncmp( "nodata", Tok, 6 ) ) {
  45.                 switches |= SW_NODATA;
  46.                 StopCopy();
  47.                 StartCopy();
  48.             } else {
  49.             if( TokLen == 6 && !strncmp( "custom", Tok, 6 ) ) {
  50.                 switches |= SW_CUSTOM;
  51.                 StopCopy();
  52.                 StartCopy();
  53.             } else {
  54.             if( TokLen == 8 && !strncmp( "presuper", Tok, 8 ) ) {
  55.                 switches |= SW_PRESUPER;
  56.                 StopCopy();
  57.                 StartCopy();
  58.             } else {
  59.             if( TokLen == 9 && !strncmp( "postsuper", Tok, 9 ) ) {
  60.                 switches |= SW_POSTSUPER;
  61.                 StopCopy();
  62.                 StartCopy();
  63.             } else {
  64.             if( TokLen == 10 && !strncmp( "supercheck", Tok, 10 ) ) {
  65.                 switches |= SW_SUPERCHECK;
  66.                 StopCopy();
  67.                 StartCopy();
  68.             } else {
  69.             if( TokLen == 11 && !strncmp( "noearlydata", Tok, 11 ) ) {
  70.                 switches |= SW_NOEARLYDATA;
  71.                 StopCopy();
  72.                 StartCopy();
  73.             } else {
  74.             if( TokLen == 9 && !strncmp( "cleardata", Tok, 9 ) ) {
  75.                 switches |= SW_CLEARDATA;
  76.                 StopCopy();
  77.                 StartCopy();
  78.             } else {
  79.             if( TokLen == 5 && !strncmp( "super", Tok, 5 ) ) {
  80.                 switches |= SW_SUPER;
  81.                 StopCopy();
  82.                 StartCopy();
  83.             } else switches = 0;
  84.             }
  85.             }
  86.             }
  87.             }
  88.             }
  89.             }
  90.             }
  91.             }
  92.             }
  93.             }
  94.         }
  95.  
  96.         
  97.     }
  98.     
  99.     FScan( ClassDef, c1, &ClassList ) {
  100.         if( !stricmp( sfname, c1->Name ) ) {
  101.             cd = c1;
  102.             CreateDisp( ofh );
  103.         }
  104.     }
  105.     
  106.     return 1;
  107.  
  108. }
  109.  
  110. short ParseBC::DoMA( short attr )
  111. {
  112.  short BLevel;
  113.  short mdspec = 0;
  114.  MethodDef *md;
  115.  
  116.     reppar.Clear();
  117.     clref.Clear();
  118.  
  119.     StopCopy();
  120.  
  121.     GetToken();
  122.     if( !TokLen ) return 0;
  123.     
  124.     if( attr ) {
  125.     
  126.         switches |= SW_CUSTOM;
  127.         
  128.         if( TokLen == 3 && !strncmp( "Set", Tok, 3 ) ) switches |= SW_SET;
  129.         else {
  130.         if( TokLen == 3 && !strncmp( "Get", Tok, 3 ) ) switches |= SW_GET;
  131.         else {
  132.         if( TokLen == 4 && !strncmp( "Init", Tok, 4 ) ) switches |= SW_INIT;
  133.         else {
  134.             Error( 10 );
  135.             return 0;
  136.         }
  137.         }
  138.         }
  139.  
  140.         GetToken();
  141.         if( !TokLen ) return 0;
  142.  
  143.     }
  144.         
  145.     cd = (ClassDef*)(ClassList.FindItem( Tok, TokLen ));
  146.     
  147.     if( !cd ) {
  148.         Error( 6 );
  149.         return 0;
  150.     }
  151.  
  152.     GetToken();
  153.     if( !TokLen ) return 0;
  154.     
  155.     if( TokLen != 2 || strncmp( Tok, "::", 2 ) ) {
  156.         Error( 7 );
  157.         return 0;
  158.     }
  159.  
  160.     GetToken();
  161.     if( !TokLen ) return 0;
  162.  
  163.     BLevel = MBracket;
  164.  
  165.     switches |= stricmp( sfname, cd->Name ) ? 0 : SW_LOCAL;
  166.     if( switches & SW_LOCAL ) fprintf( ofh, "static " );
  167.  
  168.     if( !attr ) {
  169.  
  170.     if( TokLen == strlen( cd->Name ) && !strncmp( cd->Name, Tok, TokLen ) ) {
  171.         md = new MethodDef( "OM_NEW", 6, 0, switches );
  172.         cd->AddTail( (Family*)md );
  173.         mdspec = 1;
  174.     } else {
  175.  
  176.     if( TokLen == strlen( cd->Name )+1 && *Tok == '~' && !strncmp( cd->Name, Tok+1, TokLen-1 ) ) {
  177.         md = new MethodDef( "OM_DISPOSE", 10, 0, switches );
  178.         cd->AddTail( (Family*)md );
  179.         mdspec = 2;
  180.     } else {
  181.  
  182.     if( switches & SW_SUPER ) {
  183.         md = new MethodDef( Tok, TokLen, 0, switches );
  184.         cd->AddTail( (Family*)md );
  185.     } else {
  186.         md = (MethodDef*)(cd->FindItem( Tok, TokLen ));
  187.         md->switches |= switches;
  188.     }
  189.     
  190.     }
  191.     
  192.     }
  193.     
  194.     if( !md ) {
  195.         Error( 8 );
  196.         return 0;
  197.     }
  198.     
  199.     if( md->switches & SW_VIRTUAL ) {
  200.         Error( 17 );
  201.         return 0;
  202.     }
  203.     
  204.     switches |= md->switches;
  205.     
  206.     short CLevel = CBracket;
  207.             
  208.     GetToken();
  209.     if( !TokLen ) return 0;
  210.             
  211.     if( !chcmp( '(' ) ) {
  212.         Error( 5 );
  213.         return 0;
  214.     }
  215.  
  216.     /* Parameters */
  217.     
  218.     char pbuf[100];
  219.     pbuf[0] = 0;
  220.     short i1 = 0, i2 = 0, iscl = 0;
  221.     long num = 1;
  222.     InitClassPtrDef();
  223.     
  224.     while( 1 ) {
  225.     
  226.     beg_par:
  227.         GetToken();
  228.         if( !TokLen ) return 0;
  229.         
  230.         short ret;
  231.         ret = ClassPtrDefinition( &clref, 0, 1 );
  232.         if( ret == -1 ) return 0;
  233.         if( ret ) iscl = 1;
  234.  
  235.         if( ( chcmp( ')' ) && CBracket == CLevel ) || chcmp( ',' ) ) {
  236.         
  237.             if( chcmp( ')' ) && num == 1 && !i1 ) break;
  238.  
  239.             if( i2 <= 1 ) {
  240.                 Error( 13 );
  241.                 return 0;
  242.             }
  243.             
  244.             char prep[50], ptype[30];
  245.             if( iscl ) strcpy( ptype, "Object*" );
  246.             else {
  247.                 memcpy( ptype, pbuf+1, i2-2 );
  248.                 ptype[i2-2] = 0;
  249.             }
  250.             iscl = 0;
  251.             sprintf( prep, "((%s)((ULONG*)msg)[%ld])", ptype, num );
  252.             reppar.Add( pbuf+i2, i1-i2, prep, 0 );
  253.             
  254.             i1 = i2 = 0;
  255.             num++;
  256.             if( chcmp( ')' ) ) break;
  257.             goto beg_par;
  258.         }    
  259.         
  260.         pbuf[i1] = ' ';
  261.         i1++;
  262.         memcpy( pbuf + i1, Tok, TokLen );
  263.  
  264.         i2 = i1;
  265.         i1 += TokLen;
  266.         
  267.  
  268.     }
  269.     
  270.     fprintf( ofh, "unsigned long m%s%s( struct IClass *cl, Object *obj, %s msg )\n", cd->Name, md->Name, md->msgtype );
  271.     
  272.     fprintf( ofh, "{\n" );
  273.  
  274.     short sbrc = SBracket;
  275.  
  276.     GetToken();
  277.     if( !TokLen ) return 0;
  278.  
  279.     /* early code */    
  280.     InitClassPtrDef();
  281.     if( chcmp( '[' ) ) {
  282.         StartCopy();
  283.         while( 1 ) {
  284.  
  285.             GetToken();
  286.             if( !TokLen ) return 0;
  287.             
  288.             if( chcmp( ']' ) && sbrc == SBracket ) break;
  289.  
  290.             if( ClassPtrDefinition( &clref ) == -1 ) return 0;
  291.             
  292.         }
  293.         StopCopy();
  294.  
  295.         GetToken();
  296.         if( !TokLen ) return 0;
  297.  
  298.     }
  299.     
  300.     if( !(switches & SW_CUSTOM) ) {
  301.     
  302.         if( mdspec == 1 ) fprintf( ofh, " unsigned long _ret;\n" );
  303.         else fprintf( ofh, " unsigned long _ret = 1;\n" );
  304.     
  305.     /* OM_NEW */
  306.     if( mdspec == 1 ) {
  307.     
  308.         if( chcmp( ':' ) ) {
  309.  
  310.             InsertIAttrPre( ofh );
  311.             
  312.             if( !(switches & SW_NODATA) ) {
  313.                 if( switches & SW_NOEARLYDATA ) {
  314.                     fprintf( ofh, " %sData *data;\n", cd->Name );
  315.                 } else {
  316.                     fprintf( ofh, " %sData *data, _tdata;\n data = &_tdata;\n", cd->Name );
  317.                     if( switches & SW_CLEARDATA ) fprintf( ofh, " memset( data, 0, sizeof( %sData ) );\n", cd->Name );
  318.                 }
  319.             }
  320.  
  321.             fprintf( ofh, " obj = (Object*)BCC_DoSuperNew( cl, obj,\n" );
  322.  
  323.             StartCopy();
  324.         
  325.             while( 1 ) {
  326.             
  327.                 GetToken();
  328.                 if( !TokLen ) return 0;
  329.  
  330.                 short rcr;
  331.                 rcr = FullCheck( );
  332.                 if( rcr == -1 ) return 0;
  333.  
  334.                 if( chcmp( '{' ) ) break;
  335.  
  336.             }
  337.             
  338.             StopCopy();
  339.             
  340.             fprintf( ofh, ",\n TAG_MORE, (unsigned long)msg->ops_AttrList,\n TAG_DONE );\n" );
  341.             
  342.             InsertIAttr( ofh );
  343.             
  344.             fprintf( ofh, " _ret = (unsigned long)obj;\n if( !obj ) return 0;\n" );
  345.             if( !(switches & SW_NODATA ) ) {
  346.                 if( switches & SW_NOEARLYDATA ) {
  347.                     fprintf( ofh, " data = INST_DATA( cl, obj );\n", cd->Name );
  348.                     if( switches & SW_CLEARDATA ) fprintf( ofh, " memset( data, 0, sizeof( %sData ) );\n", cd->Name );
  349.                 } else fprintf( ofh, " data = INST_DATA( cl, obj );\n memcpy( data, &_tdata, sizeof( %sData ) );\n", cd->Name );
  350.             }
  351.         } else {
  352.             InsertIAttrPre( ofh );
  353.             if( !(switches & SW_NODATA) ) {
  354.                 if( cont ) {
  355.                     fprintf( ofh, " %sData *data, _tdata;\n data = &_tdata;\n", cd->Name );
  356.                     if( switches & SW_CLEARDATA ) fprintf( ofh, " memset( data, 0, sizeof( %sData ) );\n", cd->Name );
  357.                 } else fprintf( ofh, " %sData *data;\n", cd->Name );
  358.             }
  359.             InsertIAttr( ofh );
  360.             fprintf( ofh, " obj = (Object*)DoSuperMethodA( cl, obj, (Msg)msg );\n" );
  361.             fprintf( ofh, " _ret = (unsigned long)obj;\n if( !obj ) return 0;\n" );
  362.             if( !(switches & SW_NODATA ) ) {
  363.                 if( cont ) {
  364.                     fprintf( ofh, " data = INST_DATA( cl, obj );\n memcpy( data, &_tdata, sizeof( %sData ) );\n", cd->Name );
  365.                 } else {
  366.                     fprintf( ofh, " data = INST_DATA( cl, obj );\n" );
  367.                     if( switches & SW_CLEARDATA ) fprintf( ofh, " memset( data, 0, sizeof( %sData ) );\n", cd->Name );
  368.                 }
  369.             }
  370.         }
  371.     /* Other method */
  372.     } else {
  373.         if( !(switches & SW_NODATA ) ) fprintf( ofh, " %sData *data = INST_DATA( cl, obj );\n", cd->Name );
  374.         if( switches & SW_PRESUPER ) {
  375.             fprintf( ofh, " _ret = DoSuperMethodA( cl, obj, (Msg)msg );\n" );
  376.             if( switches & SW_SUPERCHECK ) fprintf( ofh, " if( !_ret ) return 0;\n" );
  377.         }
  378.     }
  379.     
  380.     }
  381.     
  382.     } else {
  383.     /* Attribute mode */
  384.     
  385.         VarDef *vd;
  386.  
  387.         if( switches & SW_SUPER ) {
  388.             vd = new VarDef( Tok, TokLen, switches );
  389.             cd->Var.AddTail( (Family*)vd );
  390.         } else 
  391.         if( !(vd = (VarDef*)((TextItem*)&cd->Var)->FindItem( Tok, TokLen )) ) {
  392.             Error( 11 );
  393.             return 0;
  394.         }
  395.         
  396.         vd->switches |= switches;
  397.  
  398.         if( vd->switches & SW_SIMPLE ) {
  399.             Error( 14 );
  400.             return 0;
  401.         }
  402.         
  403.         if( vd->switches & SW_VIRTUAL ) {
  404.             Error( 17 );
  405.             return 0;
  406.         }
  407.  
  408.  
  409.         fprintf( ofh, "void a%s%s", cd->Name, vd->Name );
  410.         if( vd->switches & SW_GET ) {
  411.             fprintf( ofh, "Get( struct IClass *cl, Object *obj, " );
  412.         } else {
  413.             if( vd->switches & SW_SET ) fprintf( ofh, "Set" );
  414.             else fprintf( ofh, "Init" );
  415.             fprintf( ofh, "( struct IClass *cl, Object *obj, " );
  416.         }
  417.  
  418.         short CLevel = CBracket;
  419.         
  420.         GetToken();
  421.         if( !TokLen ) return 0;
  422.         
  423.         if( chcmp( '(' ) ) {
  424.         
  425.             StartCopy();
  426.         
  427.             short s_par, e_par = 0, e_typ = 0, f_time = 1, c_line;
  428.             
  429.             c_line = LineN;
  430.             s_par = TokStart+TokLen;
  431.         
  432.             while( 1 ) {
  433.     
  434.                 GetToken();
  435.                 if( !TokLen ) return 0;
  436.                 
  437.                 if( c_line != LineN ) {
  438.                     Error( 21 );
  439.                     return 0;
  440.                 }
  441.                 
  442.                 short ret;
  443.                 ret = ClassPtrDefinition( &clref );
  444.                 if( ret == -1 ) return 0;
  445.  
  446.                 if( chcmp( ')' ) && CBracket == CLevel ) break;
  447.  
  448.                 e_typ = e_par;
  449.                 e_par = TokStart;
  450.                 
  451.             }
  452.             
  453.             StopCopy();
  454.             
  455.             if( e_par ) {
  456.                 memcpy( vd->ParType, LineBuf+s_par, e_par - s_par );
  457.                 vd->ParType[e_par-s_par] = 0;
  458.             }
  459.  
  460.             GetToken();
  461.             if( !TokLen ) return 0;
  462.             
  463.         } else {
  464.             if( vd->switches & SW_GET ) {
  465.                 fprintf( ofh, " unsigned long *store" );
  466.                 strcpy( vd->ParType, "unsigned long*" );
  467.             }
  468.             else {
  469.                 fprintf( ofh, " unsigned long value" );
  470.                 strcpy( vd->ParType, "unsigned long" );
  471.             }
  472.         }
  473.         
  474.  
  475.         
  476.         fprintf( ofh, " )\n{\n" );
  477.  
  478.         if( !(switches & SW_NODATA ) ) fprintf( ofh, " %sData *data = INST_DATA( cl, obj );\n", cd->Name );
  479.  
  480.     }
  481.  
  482.     if( !chcmp( '{' ) ) {
  483.         Error( 2 );
  484.         return 0;
  485.     }
  486.  
  487.     fprintf( ofh, " /* UC Beg */\n" );
  488.  
  489.     StartCopy();
  490.     
  491.     short wasmret = 0;
  492.     
  493.     char objrefbuf[30];
  494.     strcpy( objrefbuf, cd->type );
  495.     strcpy( objrefbuf+3, cd->Name );
  496.     clref.Add( "obj", 3, objrefbuf, 0 );
  497.  
  498.     InitClassPtrDef();
  499.  
  500.     /* Inside code */        
  501.     while( 1 ) {
  502.     
  503.         again:
  504.  
  505.         GetToken();
  506.         if( !TokLen ) break;
  507.         
  508.         short rcr;
  509.  
  510.         rcr = FullCheck( );
  511.         if( rcr == -1 ) return 0;
  512.         if( rcr ) goto again;
  513.  
  514.         if( chcmp( '}' ) && MBracket == BLevel ) break;
  515.  
  516.         rcr = ClassPtrDefinition( &clref );
  517.         if( rcr == -1 ) return 0;
  518.         if( rcr ) goto again;
  519.  
  520.         if( TokLen == 7 && !strncmp( Tok, "mreturn", 7 ) ) {
  521.             StopCopy();
  522.             fprintf( ofh, "{ _ret = " );
  523.             StartCopy();
  524.             
  525.             while( 1 ) {
  526.                 GetToken();
  527.                 if( !TokLen ) return 0;
  528.                 
  529.                 if( chcmp( ';' ) ) break;
  530.             }
  531.             
  532.             StopCopy();
  533.             StartCopy();
  534.             
  535.             fprintf( ofh, "; goto %s_exit; } ", md->Name );
  536.             wasmret = 1;
  537.         }
  538.     
  539.     }
  540.  
  541.     fprintf( ofh, "\n /* UC End */\n" );
  542.     
  543.     if( !(switches & SW_CUSTOM) ) {
  544.     
  545.         fprintf( ofh, "%s_exit:\n", md->Name );
  546.  
  547.         if( mdspec == 1 ) {
  548.             if( wasmret ) fprintf( ofh, " if( !_ret ) CoerceMethod( cl, obj, OM_DISPOSE );\n" );
  549.         } else
  550.         if( switches & SW_POSTSUPER ) {
  551.             if( switches & SW_SUPERCHECK ) fprintf( ofh, " if( !_ret ) return 0;\n" );
  552.             fprintf( ofh, " _ret = DoSuperMethodA( cl, obj, (Msg)msg );\n" );
  553.         }
  554.         
  555.         fprintf( ofh, "return _ret;\n" );
  556.     
  557.     }
  558.  
  559.     return 1;
  560.  
  561. }
  562.